package com.fengshang.wedding.domain.service.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import org.apache.log4j.Logger;

import com.fengshang.wedding.domain.model.api.ITopic;
import com.fengshang.wedding.domain.model.api.Topic;
import com.fengshang.wedding.domain.model.api.VisibilityLevel;
import com.fengshang.wedding.domain.model.internal.TopicEntity;
import com.fengshang.wedding.domain.service.api.ITopicManager;
import com.fengshang.wedding.domain.service.api.InvalidTopicException;
import com.fengshang.wedding.domain.service.api.TopicNotFoundException;
import com.fengshang.wedding.utils.DateFormatterHelper;
/**
 * 
 *@author gembin@gmail.com
 * 
 */
public class TopicManager implements ITopicManager {
	private static Logger logger=Logger.getLogger(TopicManager.class);
	ITopicPersistenceService topicPersistenceService;
	
	public void setTopicPersistenceService(
			ITopicPersistenceService topicPersistenceService) {
		this.topicPersistenceService = topicPersistenceService;
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#createTopic(int, com.fengshang.wedding.domain.model.api.ITopic)
	 */
	public void createTopic(int userId, ITopic topic) throws InvalidTopicException{
		((Topic) topic).setPostDate(DateFormatterHelper.format(new Date()));
		TopicEntity topicEntity=new TopicEntity();
		topicEntity.importData(topic);
		try{
			topicPersistenceService.createTopic(userId, topicEntity);
		}catch(EntityNotFoundException e){
			throw new InvalidTopicException("a topic should belong to an existing user!!",e);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#findUserTopicsByPagination(int, int, int)
	 */
	public Collection<ITopic> findUserTopicsByPagination(int userId,
			int pageSize, int currentPage) {
		Collection<TopicEntity> topicsEntity=topicPersistenceService.findUserTopicsByPagination(userId, pageSize, currentPage);
		Collection<ITopic> topicsVo=new ArrayList<ITopic>();
		entityToValueObject(topicsEntity,topicsVo);
		return topicsVo;  
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#findTopicsByPagination(java.lang.String, int, int)
	 */
	public Collection<ITopic> findTopicsByPagination(String query,
			int pageSize, int currentPage) {
		Collection<TopicEntity> topicsEntity=topicPersistenceService.findTopicsByPagination(query, pageSize, currentPage);
		Collection<ITopic> topicsVo=new ArrayList<ITopic>();
		entityToValueObject(topicsEntity,topicsVo);
		return topicsVo;  
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#findVisibleTopicsByPagination(int, int, int)
	 */
	public Collection<ITopic> findVisibleTopicsByPagination(int userId,
			int pageSize, int currentPage) {
		String query="select t from Topic t , User u  where t.owner <> "+userId+" AND u.id="+userId+" AND t.visibilityLevel >= u.role";
		Collection<TopicEntity> topicsEntity=topicPersistenceService.findTopicsByPagination(query, pageSize, currentPage);
		Collection<ITopic> topicsVo=new ArrayList<ITopic>();
		entityToValueObject(topicsEntity,topicsVo);
		return topicsVo;  
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#removeTopic(int)
	 */
	public void removeTopic(int topicId)throws TopicNotFoundException {
		try{
			topicPersistenceService.removeTopic(topicId);
		}catch (EntityNotFoundException e){
			throw new TopicNotFoundException(e);
		}
		
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#updateTopic(com.fengshang.wedding.domain.model.api.ITopic)
	 */
	public ITopic updateTopic(ITopic topic)throws TopicNotFoundException{
		if(topic==null)return null;
		int topicId=topic.getId();
		TopicEntity topicEntity=topicPersistenceService.findTopicById(topicId);
		if(topicEntity==null)
			 throw new TopicNotFoundException("Topic with id="+topicId+" dose not exists!");
		topicEntity.importData(topic);
		return topicPersistenceService.updateTopic(topicEntity).getValueObject();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#getTotalPages(int)
	 */
	public int getTotalPages(int pageSize) {
		return topicPersistenceService.getTotalPages(pageSize);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#getTotalPages()
	 */
	public int getTotalPages() {
		return topicPersistenceService.getTotalPages();
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.ITopicManager#updateVisibilityLevel(int, com.fengshang.wedding.domain.model.api.VisibilityLevel)
	 */
	public void updateVisibilityLevel(int topicId, VisibilityLevel level) {
		TopicEntity topicEntity=topicPersistenceService.findTopicById(topicId);
		if(topicEntity==null)
			 throw new TopicNotFoundException("Topic with id="+topicId+" dose not exists!");
		topicEntity.setVisibilityLevel(level);
		topicPersistenceService.updateTopic(topicEntity);
	}
	/**
	 * entity to vo
	 * @param topicsEntity
	 * @param topicsVo
	 */
	private static void entityToValueObject(final Collection<TopicEntity> topicsEntity,final Collection<ITopic> topicsVo){
		logger.debug("Topic Size:"+topicsEntity.size());
		for(Iterator<TopicEntity> it=topicsEntity.iterator();it.hasNext();){
			TopicEntity topic=it.next();
			logger.debug("Topic Owner:"+topic.getOwner().getValueObject());
			Date postDate=topic.getPostDate();
			topic.setPostDate(DateFormatterHelper.format(postDate));
			topicsVo.add(topic.getValueObject());
		}
	}

}
